<?
session_start();
include("connect.php");

function createDateRangeArray($strDateFrom,$strDateTo) {
  // takes two dates formatted as YYYY-MM-DD and creates an
  // inclusive array of the dates between the from and to dates.
  // could test validity of dates here but I'm already doing
  // that in the main script

  $aryRange=array();

  $iDateFrom=mktime(1,0,0,substr($strDateFrom,5,2),     substr($strDateFrom,8,2),substr($strDateFrom,0,4));
  $iDateTo=mktime(1,0,0,substr($strDateTo,5,2),     substr($strDateTo,8,2),substr($strDateTo,0,4));

  if ($iDateTo>=$iDateFrom) {
    array_push($aryRange,date('Y-m-d',$iDateFrom)); // first entry

    while ($iDateFrom<$iDateTo) {
      $iDateFrom+=86400; // add 24 hours
      array_push($aryRange,date('Y-m-d',$iDateFrom));
    }
  }
  return $aryRange;
}


if($_GET['request']=="UPDATE_ITEM"){
		$sql="UPDATE `daily-expense` SET item='$_GET[txt]' where id=$_GET[id]";
		mysql_query($sql);
		$iid=mysql_insert_id();
		echo $iid;
}elseif($_GET['request']=="DEL_ITEM"){
		$sql="DELETE from `daily-expense` WHERE id=$_GET[id]";
		mysql_query($sql);
}elseif($_GET['request']=="UPDATE_AMT"){
	$sql="UPDATE `daily-expense` SET amount='$_GET[txt]' where id=$_GET[id]";
	mysql_query($sql);
}elseif($_GET['request']=="ADD_ITEM" ){
	$sql="INSERT INTO `daily-expense` (`item` ,amount,`userid` ,`date`,`emailid`)	VALUES ('$_GET[txt]',0.00 ,'$_SESSION[userid]', '$_GET[date]','$_SESSION[emailid]')";
	mysql_query($sql);
  $id=mysql_insert_id();
	echo "$id";	

}else if($_GET['request']=="GET_TOTAL"){
	$stDt=date('Y-m-d', strtotime($_GET[startDt]));
	$enDt=date('Y-m-d', strtotime($_GET[endDt])+(24*60*60));
	
	$sql2="SELECT sum(amount) as totalAmt FROM `daily-expense` WHERE userid='$_SESSION[userid]' AND date >= '$stDt' AND date <= '$enDt'"; 
	$res=mysql_query($sql2);
	$row=mysql_fetch_array($res);
	echo number_format(round($row['totalAmt'],2),2);
}

if($_GET['request']=="TODAY_DT"){
	echo date("m-d-y",time());
}

if($_GET['request']=="UPDT_USR"){
$msg="Hello Friend,\n\n
	Thanks for using this Eddytools Daily Expense Tracking Google Gadget.\n
	Here is your account information\n
	email id: $_SESSION[emailid]\n
	account id: $_SESSION[userid]\n
	If you want to view your expense data in this gadget then update settings with this information.\n
	(By default your information is saved in iGoogle for session.)\n\n
	Have Fun!!!\n
	Eddytools Software Solution
	";
	$sql="SELECT emailid FROM `daily-expense-user` WHERE userid='$_GET[usrid]'";
	$res=mysql_query($sql);	
	$countemail=mysql_num_rows($res);
	if($countemail==0){
		$sql="INSERT INTO `daily-expense-user` (`userid` ,`emailid` ,`doj` ,`last_access`)VALUES ('$_GET[usrid]', '$_GET[emailid]', NOW( ) , NOW( ))";
	}else{
		$row=mysql_fetch_array($res);
		$sql=" UPDATE `daily-expense-user` SET `emailid` = '$_GET[emailid]',`last_access` = NOW( ) WHERE `userid` ='$_GET[usrid]'";
	}
	mysql_query($sql);
	echo $_GET[emailid];
	mail("$_SESSION[emailid]","Google Gadget: Daily Expense Tracking  (Account Information)",$msg ,"From:Eddytools<contact@eddytools.com>");
	mail("contact@eddytools.com","Google Gadget: Daily Expense Tracking  (Account Information)",$msg ,"From:Eddytools<contact@eddytools.com>");
}
if($_GET['request']=="SETUSR"){
	$_SESSION[userid]=$_GET['usrid'];
	$sql="SELECT emailid FROM `daily-expense-user` WHERE userid='$_SESSION[userid]'";
	$res=mysql_query($sql);	
	$countemail=mysql_num_rows($res);
	if($countemail!=0){
		$row=mysql_fetch_array($res);
		$eid=$row['emailid'];
		$_SESSION[emailid]=$eid;
	}else{
		$eid="";
	}
	echo $eid;
}

if($_GET['request']=="LOAD_REPORT"){
	$stDt=date('Y-m-d', strtotime($_GET[startDt]));
	$enDt=date('Y-m-d', strtotime($_GET[endDt]));

$rowRet="";
	foreach(createDateRangeArray($stDt,$enDt) as $dts ){
	$sql="SELECT * FROM `daily-expense` WHERE userid='$_SESSION[userid]' AND date >= '$dts 00:00:00' AND date <= '$dts 23:59:59' AND item LIKE '%$_GET[keyword]%' order by date"; 
	$res=mysql_query($sql);
	
	$sql2="SELECT sum(amount) as daytotal FROM `daily-expense` WHERE userid='$_SESSION[userid]' AND date >= '$dts 00:00:00' AND date <= '$dts 23:59:59' AND item LIKE '%$_GET[keyword]%' order by date"; 
	$res2=mysql_query($sql2);
	$row2=mysql_fetch_array($res2);
	$dtot=number_format($row2[daytotal],2);
	$rowRet.= "<div id=\"dt_row_$dts\"><div class=\"dt\" id=\"dt\"><strong>$dts</strong><span class=\"add_new hand\" dt=\"$dts\">[ <img id=\"additem_icon_$dts\"  src=\"images/add-icon.jpg \"> Add Item]&nbsp;&nbsp;</span>($dtot) </div><div class=\"dt_amt\"></div></div>";
		while($row=mysql_fetch_array($res)){
			//$rowRet.= "<div id=\"row_$row[id]\" class=\"item_row\"><div class=\"g_item\" id=\"$row[id]\">$row[item]</div><div class=\"g_amt\" id=\"a_$row[id]\">$row[amount]</div></div>";
			$rowRet.="<div id=\"row_$row[id]\" class=\"item_row\">
							<span class=\"g_item\" id=\"$row[id]\">$row[item]</span>
							<span class=\"g_amt\" id=\"a_$row[id]\">$row[amount]</span>
							<span class=\"g_del_ico hand\" id=\"del_$row[id]\">x</span></div>";
						
		}
	}
	echo "$rowRet";			 
}
//#################################Admin Report######################################//
if($_GET['request']=="ADMIN_LOAD_REPORT"){
	$stDt=date('Y-m-d', strtotime($_GET[stDate]));
	$enDt=date('Y-m-d', strtotime($_GET[endDate]));

$rowRet="";
	foreach(createDateRangeArray($stDt,$enDt) as $dts ){
	$sql="SELECT * FROM `daily-expense` WHERE  date >= '$dts 00:00:00' AND date <= '$dts 23:59:59' order by date desc"; 
	$res=mysql_query($sql);
	$count=mysql_num_rows($res);
	echo $dts."-".$count."<br>";
	
	while($row=mysql_fetch_array($res)){
			$rowRet.="<div id=\"row_$row[id]\" class=\"item_row\">
							<span class=\"g_item\" id=\"$row[id]\">$row[item]</span>
							<span class=\"g_amt\" id=\"a_$row[id]\">$row[amount]</span>
							<span class=\"g_amt\" id=\"a_$row[id]\">$row[emailid]</span>
							<span class=\"g_amt\" id=\"a_$row[id]\">$row[userid]</span>
							<span class=\"g_amt\" id=\"a_$row[id]\">$row[date]</span>
							</div><hr>";
						
		}
	}
	echo "$rowRet";			 
}

if($_GET['request']=="ADMIN_LOAD_USER"){
	
$rowRet="";
	$sql="SELECT * FROM `daily-expense-user`"; 
	$res=mysql_query($sql);
	$count=mysql_num_rows($res);
	echo $count;
	while($row=mysql_fetch_array($res)){
			$rowRet.="<div id=\"row_$row[id]\" class=\"item_row\">
							<span class=\"g_item\" id=\"$row[id]\">$row[userid]</span>
							<span class=\"g_amt\" id=\"a_$row[id]\">$row[emailid]</span>
							<span class=\"g_amt\" id=\"a_$row[id]\">$row[doj]</span>
							<span class=\"g_amt\" id=\"a_$row[id]\">$row[last_access]</span>
							</div><hr>";
						
		}
	echo "$rowRet";			 
}
?>
